<?php

/*
	info: 权限管理
	date: 2011-11-20
*/

if (!defined('ADMIN_WEBINROOT'))
{
	header("HTTP/1.0 404 Not Found");
	exit();
}

$webglobal['page_purview'] = array();

//权限分组列表
if ($webglobal['mode'] == 'purviewgroup') {
	$query = $db->query("SELECT * FROM {$webconfig['dbpre']}purview WHERE upid = 0 ORDER BY `ORDER` DESC, id ASC");

	$webglobal['page_purview']['dataarray'] = array();
	$temptr = 1;
	while ($tempData = $db->FetchArray($query)) {
		$tempData['tdclass'] = (($temptr++)%2==0) ? ' class="evenrow"' : '';
		$webglobal['page_purview']['dataarray'][$tempData['id']] = $tempData;
	}

//添加权限分组
} elseif ($webglobal['mode'] == 'purviewgroupadd') {
	if (global_CheckSubmit('adminsubmit')) {
		$webrequest['name'] = isset($webrequest['name']) ? trim($webrequest['name']) : '';
		$webrequest['order'] = isset($webrequest['order']) ? intval($webrequest['order']) : 0;
		if ($webrequest['name'] == '') {
			admin_ShowMessage('分组标题不能为空！', '', 'BACK');
		} else {
			admin_CheckFromForm(ADMIN_WEBURLPATH."/index.php?mode=purviewgroup");
			$db->Query("INSERT INTO {$webconfig['dbpre']}purview(`upid`, `name`, `mode`, `linkmore`, `order`) VALUES('0', '{$webrequest['name']}', '', '','{$webrequest['order']}')");
			$tempnewsid = $db->InsertId();

			admin_AddLog("添加权限分组|id: {$tempnewsid}; name: {$webrequest['name']}");
			admin_ShowMessage('权限分组添加成功。', ADMIN_WEBURLPATH."/index.php?mode=purviewgroup");
		}
	}
	else
	{
		admin_StartFromForm();
	}

//修改权限分组
} elseif ($webglobal['mode'] == 'purviewgroupedit') {
	if (!global_CheckCome()) {
		admin_ShowMessage('参数错误。', '', 'BACK');
	}

	$tempdelstr = '';
	/*
	if (isset($webrequest['delid']) && is_array($webrequest['delid']) && !empty($webrequest['delid'])) {
		$tempdelstr = implode(',', global_Intval($webrequest['delid']));
		//删除权限分组以及分组下的所有权限
		$db->Query("DELETE FROM {$webconfig['dbpre']}purview WHERE id IN ({$tempdelstr}) OR upid IN ({$tempdelstr})");
	}
	*/
	$tempeditarray = array();
	if (isset($webrequest['name']) && is_array($webrequest['name']) && !empty($webrequest['name'])) {
		$temphaveid = implode(',', global_Intval(array_keys($webrequest['name'])));

		//查询修改前的数据
		$query = $db->Query("SELECT id, name, `order` FROM {$webconfig['dbpre']}purview WHERE id IN ({$temphaveid}) AND upid = 0");
		$tempOldData = array();
		while ($tempdata = $db->FetchArray($query)) {
			$tempOldData[$tempdata['id']] = $tempdata;
		}

		foreach ($webrequest['name'] as $tempeditid => $tempeditvalue) {
			if (isset($tempOldData[$tempeditid])) {
				$webrequest['name'][$tempeditid] = isset($webrequest['name'][$tempeditid]) ? trim($webrequest['name'][$tempeditid]) : '';
				$webrequest['order'][$tempeditid] = isset($webrequest['order'][$tempeditid]) ? intval($webrequest['order'][$tempeditid]) : 0;

				if (($webrequest['name'][$tempeditid] != '' && ($webrequest['name'][$tempeditid] != $tempOldData[$tempeditid]['name'])) || $tempOldData[$tempeditid]['order'] != $webrequest['order'][$tempeditid]) {
					$db->Query("UPDATE {$webconfig['dbpre']}purview SET name = '{$webrequest['name'][$tempeditid]}', `order` = '{$webrequest['order'][$tempeditid]}' WHERE id = '{$tempeditid}'");
					$tempeditarray[] = $tempeditid;
				}
			}
		}
	}
	$templogmsg = '';
	if (!empty($tempdelstr)) {
		$templogmsg .= "; del: {$tempdelstr}";
	}
	if (!empty($tempeditarray)) {
		$templogmsg .= "; edit: ".implode(',', $tempeditarray);
	}
	if (empty($templogmsg)) {
		admin_ShowMessage("没有任何条目被修改，系统自动返回。", ADMIN_WEBURLPATH."/index.php?mode=purviewgroup");
	}
	admin_AddLog("批量更改权限分组|{$templogmsg}");
	admin_ShowMessage('批量更改权限分组成功。', ADMIN_WEBURLPATH."/index.php?mode=purviewgroup");
//权限列表
} elseif ($webglobal['mode']=='purview') {
	
	$webrequest['order'] = isset($webrequest['order']) ? trim($webrequest['order']) : '';
	$webglobal['page_purview']['gourl'] = "page={$webrequest['page']}";
	if ($webrequest['order'] != '') {
		$webglobal['page_purview']['gourl'] .= "&order={$webrequest['order']}";
	}
	$webglobal['page_purview']['gourlencode'] = urlencode($webglobal['page_purview']['gourl']);

	$query = $db->query("SELECT * FROM {$webconfig['dbpre']}purview ORDER BY upid, `order` DESC, id ASC");

	$groupnum = 0;
	$purviewnum = 1;
	$webglobal['page_purview']['dataarray'] = array();
	$webglobal['page_purview']['purviewgroup'] = array();
	$lastUpid = 0;
	while ($tempData = $db->FetchArray($query)) {
		if (!$tempData['upid']) {
			$groupnum++;
			$tempData['groupnum'] = $groupnum;
			$webglobal['page_purview']['dataarray'][$tempData['id']] = $tempData;
			$webglobal['page_purview']['purviewgroup'][$tempData['id']] = $tempData;
		} else {
			if ($lastUpid && $lastUpid != $tempData['upid']) {
				$purviewnum = 1;
			}
			if (isset($webglobal['page_purview']['dataarray'][$tempData['upid']])) {
				if ($tempData['order'] < 0) {
					$tempData['tdclass'] = ' class="hiddenrow"';
				} else {
					$tempData['tdclass'] = (($purviewnum++)%2==0) ? ' class="snow"' : '';
				}
				$webglobal['page_purview']['dataarray'][$tempData['upid']]['subpurview'][$tempData['id']] = $tempData;
			}
			$lastUpid = $tempData['upid'];
		}
	}

//添加权限
} elseif ($webglobal['mode']=='purviewadd') {

	$query = $db->query("SELECT * FROM {$webconfig['dbpre']}purview WHERE upid = 0 ORDER BY `ORDER` DESC, id ASC");
	$webglobal['page_purview']['group'] = array();
	while ($tempData = $db->FetchArray($query)) {
		$webglobal['page_purview']['group'][$tempData['id']] = $tempData;
	}

	if (global_CheckSubmit('adminsubmit')) {
		$webrequest['groupid'] = isset($webrequest['groupid']) ? intval($webrequest['groupid']) : 0;
		$webrequest['name'] = isset($webrequest['name']) ? trim($webrequest['name']) : '';
		$webrequest['webmode'] = isset($webrequest['webmode']) ? trim($webrequest['webmode']) : '';
		$webrequest['linkmore'] = isset($webrequest['linkmore']) ? trim($webrequest['linkmore']) : '';
		$webrequest['order'] = isset($webrequest['order']) ? intval($webrequest['order']) : 0;
		if ($webrequest['name'] == '') {
			admin_ShowMessage('权限名称不能为空！', '', 'BACK');
		} elseif (!$webrequest['groupid']) {
			admin_ShowMessage('请选择对应的权限分组！', '', 'BACK');
		} elseif (!isset($webglobal['page_purview']['group'][$webrequest['groupid']])) {
			admin_ShowMessage('系统中没有选择的权限分组！', '', 'BACK');
		} elseif ($webrequest['webmode'] == '') {
			admin_ShowMessage('请填写对应的权限操作！', '', 'BACK');
		} else {
			admin_CheckFromForm(ADMIN_WEBURLPATH."/index.php?mode=purview");
			$db->Query("INSERT INTO {$webconfig['dbpre']}purview(`upid`, `name`, `mode`, `linkmore`, `order`) VALUES('{$webrequest['groupid']}', '{$webrequest['name']}', '{$webrequest['webmode']}', '{$webrequest['linkmore']}','{$webrequest['order']}')");
			$tempnewsid = $db->InsertId();

			admin_AddLog("添加权限|id: {$tempnewsid}; name: {$webrequest['name']}");
			admin_ShowMessage('权限添加成功。', ADMIN_WEBURLPATH."/index.php?mode=purview");
		}
	}
	else {
		
		admin_StartFromForm();
	}

//修改权限
} elseif ($webglobal['mode']=='purviewedit') {
	if (!global_CheckCome()) {
		admin_ShowMessage('参数错误。', '', 'BACK');
	}
	$tempdelstr = '';
	if (isset($webrequest['delid']) && is_array($webrequest['delid']) && !empty($webrequest['delid'])) {
		$tempdelstr = implode(',', global_Intval($webrequest['delid']));
		//删除权限分组以及分组下的所有权限
		$db->Query("DELETE FROM {$webconfig['dbpre']}purview WHERE id IN ({$tempdelstr})");
	}

	$tempeditarray = array();
	if (isset($webrequest['name']) && is_array($webrequest['name']) && !empty($webrequest['name'])) {
		$temphaveid = implode(',', global_Intval(array_keys($webrequest['name'])));

		//查询修改前的数据
		$query = $db->Query("SELECT * FROM {$webconfig['dbpre']}purview WHERE id IN ({$temphaveid}) OR upid = 0");
		$tempOldData = array();
		$tempGroupData = array();
		while ($tempdata = $db->FetchArray($query)) {
			if ($tempdata['upid'] == 0) {
				$tempGroupData[$tempdata['id']] = $tempdata;
			} else {
				$tempOldData[$tempdata['id']] = $tempdata;
			}
		}

		foreach ($webrequest['name'] as $tempeditid => $tempeditvalue) {
			if (isset($tempOldData[$tempeditid])) {
				$webrequest['groupid'][$tempeditid] = isset($webrequest['groupid'][$tempeditid]) ? intval($webrequest['groupid'][$tempeditid]) : 0;
				$webrequest['name'][$tempeditid] = isset($webrequest['name'][$tempeditid]) ? trim($webrequest['name'][$tempeditid]) : '';
				$webrequest['order'][$tempeditid] = isset($webrequest['order'][$tempeditid]) ? intval($webrequest['order'][$tempeditid]) : 0;
				$webrequest['webmode'][$tempeditid] = isset($webrequest['webmode'][$tempeditid]) ? trim($webrequest['webmode'][$tempeditid]) : '';
				
				$webrequest['linkmore'][$tempeditid] = isset($webrequest['linkmore'][$tempeditid]) ? trim($webrequest['linkmore'][$tempeditid]) : '';

				if (($webrequest['groupid'][$tempeditid] > 0 && isset($tempGroupData[$webrequest['groupid'][$tempeditid]]) && $webrequest['groupid'][$tempeditid] != $tempOldData[$tempeditid]['upid']) || ($webrequest['name'][$tempeditid] != '' && ($webrequest['name'][$tempeditid] != $tempOldData[$tempeditid]['name'])) || ($webrequest['webmode'][$tempeditid] != '' && ($webrequest['webmode'][$tempeditid] != $tempOldData[$tempeditid]['mode'])) || $tempOldData[$tempeditid]['order'] != $webrequest['order'][$tempeditid] || $tempOldData[$tempeditid]['linkmore'] != $webrequest['linkmore'][$tempeditid]) {
					$db->Query("UPDATE {$webconfig['dbpre']}purview SET upid = '{$webrequest['groupid'][$tempeditid]}', name = '{$webrequest['name'][$tempeditid]}', mode = '{$webrequest['webmode'][$tempeditid]}', linkmore = '{$webrequest['linkmore'][$tempeditid]}', `order` = '{$webrequest['order'][$tempeditid]}' WHERE id = '{$tempeditid}'");
					$tempeditarray[] = $tempeditid;
				}
			}
		}
	}
	$templogmsg = '';
	if (!empty($tempdelstr)) {
		$templogmsg .= "; del: {$tempdelstr}";
	}
	if (!empty($tempeditarray)) {
		$templogmsg .= "; edit: ".implode(',', $tempeditarray);
	}
	if (empty($templogmsg)) {
		admin_ShowMessage("没有任何条目被修改，系统自动返回。", ADMIN_WEBURLPATH."/index.php?mode=purview");
	}
	admin_AddLog("批量更改权限|{$templogmsg}");
	admin_ShowMessage('批量更改权限成功。', ADMIN_WEBURLPATH."/index.php?mode=purview");
}

template_Display('purview', 1);

?>